{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第一章 Python入门"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5\n",
      "1.5\n"
     ]
    }
   ],
   "source": [
    "a = 1.5\n",
    "b = a \n",
    "a = a+1\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面的代码中，一开始a引用了float类的一个实例，然后b通过a也引用了这个实例，引用代表着与其内存地址关联（或者相当于C语言中的指向）；因为b指向float实例1.5的地址，所以a的变化不会影响b。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "集合中的元素不可重复。Python中使用集合的主要优点是集合有一个高度优化的方法用于查找特定元素是否包含在集合中，用散列表实现。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构造列表：`list(XXX)`或者`[XXX]`\n",
    "\n",
    "构造元组：`tuple(XXX)`或者`(XXX)`\n",
    "\n",
    "构造集合：`set(XXX)`或者`{XXX}`\n",
    "\n",
    "构造字典：`dict(XXX:XXX)`或者`{XXX:XXX}`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "break：跳出循环，一般在循环中结合条件语句使用。\n",
    "\n",
    "continue：跳过这一轮循环，直接开始下一轮。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你想打印什么：什么都不想\n",
      "什么都不想\n"
     ]
    }
   ],
   "source": [
    "a = input('你想打印什么：')                        ## 函数input的用法是非常简单的，唯一的参数是提示语，获得的是字符串\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一段很有意思的控制输入的代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的年龄：-1\n",
      "输入的年龄需要为正数\n",
      "请输入你的年龄：abc\n",
      "输入无效\n",
      "请输入你的年龄：20\n"
     ]
    }
   ],
   "source": [
    "age = -1\n",
    "while age < 0:\n",
    "    try:\n",
    "        age = int(input('请输入你的年龄：'))\n",
    "        if age < 0:\n",
    "            print('输入的年龄需要为正数')\n",
    "    except:\n",
    "        print('输入无效')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "条件语句的简易写法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = b\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "b = 1\n",
    "print('a = b') if a == b else print('a != b')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面这个例子中，条件语句可以表示分段函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "n = -1\n",
    "abs_n = n if n >= 0 else -n\n",
    "print(abs_n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "遍历列表，在某种条件下生成新列表的简单形式（这称为列表解析）：先循环后条件（对向量或者列表或者数组的操作来说，R语言比较方便）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16]\n"
     ]
    }
   ],
   "source": [
    "a = [1, 2, 3, 4, -1]\n",
    "b = [k**2 for k in a if k>0]\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "练习："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "R-1.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_multiple(n, m):\n",
    "    return n%m == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(is_multiple(10, 5))\n",
    "print(is_multiple(10, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "R-1.2\n",
    "\n",
    "判断是否为偶数，不能用乘法、除法和求余。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_even(k):\n",
    "    if k < 0:\n",
    "        while k < 0:\n",
    "            k += 2\n",
    "        return k == 0\n",
    "    elif k > 0:\n",
    "        while k > 0:\n",
    "            k -= 2\n",
    "        return k == 0\n",
    "    else:\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "True\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(is_even(100))\n",
    "print(is_even(101))\n",
    "print(is_even(-100))\n",
    "print(is_even(-101))\n",
    "print(is_even(0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "R-1.3\n",
    "\n",
    "求最大值和最小值，用分治吧。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def minmax(data):\n",
    "    if len(data) == 1:                                ## 基线\n",
    "        return data[0], data[0] \n",
    "    else:                                             ## 递归\n",
    "        min_data = data[0] if data[0] < minmax(data[1:])[0] else minmax(data[1:])[0] \n",
    "        max_data = data[0] if data[0] > minmax(data[1:])[1] else minmax(data[1:])[1]\n",
    "        return min_data , max_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 450)\n",
      "(-12, 214)\n"
     ]
    }
   ],
   "source": [
    "print(minmax([1, 2, 3, 10, 102, 43, 450]))               ## 列表\n",
    "print(minmax((102, 32, 1, -12, 33, 214, 12)))            ## 元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "R-1.4 \n",
    "\n",
    "运用列表解析。对一个可迭代的对象根据某个表达式在某种条件下进行处理，生成新的可迭代对象。如果不用列表解析，就需要单独写遍历，然后一个一个`append`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_square(n):\n",
    "    return [k**2 for k in range(1, n+1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n",
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225]\n"
     ]
    }
   ],
   "source": [
    "print(list_square(10))\n",
    "print(list_square(15))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "R-1.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_square_sum(n):\n",
    "    return sum([k**2 for k in range(1, n+1)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n",
      "55\n"
     ]
    }
   ],
   "source": [
    "print(list_square_sum(3))\n",
    "print(list_square_sum(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "R-1.6\n",
    "\n",
    "对R-1.5添加条件语句即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_square_sum_odd(n):\n",
    "    return sum([k**2 for k in range(1, n+1) if k%2 == 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "35\n"
     ]
    }
   ],
   "source": [
    "print(list_square_sum_odd(3))\n",
    "print(list_square_sum_odd(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "R-1.8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_index(n, k):\n",
    "    return n+k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "list = [1, 2, 3, 4, 5]\n",
    "a = get_index(len(list), -3)\n",
    "print(list[a] == list[-3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "R-1.9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[50, 60, 70, 80]\n"
     ]
    }
   ],
   "source": [
    "list = []\n",
    "for k in range(50, 81, 10):\n",
    "    list.append(k)\n",
    "print(list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "R-1.10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8, 6, 4, 2, 0, -2, -4, -6, -8]\n"
     ]
    }
   ],
   "source": [
    "list = []\n",
    "for k in range(8, -9, -2):\n",
    "    list.append(k)\n",
    "print(list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "R-1.11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 8, 16, 32, 64, 128, 256]\n"
     ]
    }
   ],
   "source": [
    "print([2**k for k in range(1, 9)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "R-1.12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "def choice(list):\n",
    "    return list[randrange(len(list))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "print(choice([1, 2, 3, 4, 5, 6]))\n",
    "print(choice([1, 2, 3, 4, 5, 6]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def new_reverse(list):\n",
    "    new_list = []\n",
    "    for i in range(1, len(list)+1):\n",
    "        new_list.append(list[-i])\n",
    "    return new_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 4, 3, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "print(new_reverse([1, 2, 3, 4, 5]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "逆序的其他方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 4, 3, 2, 1]\n",
      "[5, 4, 3, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "list = [1, 2, 3, 4, 5]\n",
    "print(list[::-1])\n",
    "list.reverse()          ## 改变原列表的排序\n",
    "print(list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.14\n",
    "\n",
    "有关列表中是否有相同元素，不同元素等，用集合来判断。这一题的代码写的十分精彩，既使用了列表解析，还使用了简洁的条件语句，整个代码十分干练。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_different_odd(list):\n",
    "    new_list = [k for k in list if k%2 == 1]\n",
    "    return False if len(set(new_list)) <= 1 else True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(find_different_odd([1, 2, 3, 4, 5, 6]))\n",
    "print(find_different_odd([2, 4, 6, 8, 10, 12]))\n",
    "print(find_different_odd([1, 2, 4 ,6, 8, 10]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_different(list):\n",
    "    return len(list) == len(set(list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(is_different([1, 2, 3, 4, 5]))\n",
    "print(is_different([1, 2, 3, 4, 5, 1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.16\n",
    "\n",
    "`+=`和`*=`在指向的类的实例可变的情况下，会直接改变指向地址存储的实例；但如果是不可变类，则会创建新的实例。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "b = a\n",
    "b += 1\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "list_1 = [1, 2, 3]\n",
    "list_2 = list_1\n",
    "list_2 += [4, 5]\n",
    "print(list_1)\n",
    "print(list_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "def scale(data, factor):\n",
    "    for i in range(0, len(data)):\n",
    "        data[i] *= factor               ## 之所以能够通过传递的参数就改变原参数，是由*=决定的，list是可变的，所以发生了变化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6]\n"
     ]
    }
   ],
   "source": [
    "list = [1, 2, 3]                        ## 如果输入元组将报错，因为不可变\n",
    "scale(list, 2)\n",
    "print(list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 6, 12, 20, 30, 42, 56, 72, 90]\n"
     ]
    }
   ],
   "source": [
    "print([k*(k+1) for k in range(0, 10)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.19"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']\n"
     ]
    }
   ],
   "source": [
    "print([chr(k) for k in range(97, 123)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 7, 5, 6, 1, 4, 2, 8]\n"
     ]
    }
   ],
   "source": [
    "import random as r\n",
    "list = [1, 2, 3, 4, 5, 6, 7, 8]\n",
    "r.shuffle(list)\n",
    "print(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "def new_shuffle(list):\n",
    "    new_list = []\n",
    "    while len(list) > 0:\n",
    "        n = r.randint(0, len(list)-1)\n",
    "        new_list.append(list[n])\n",
    "        del list[n]\n",
    "    return new_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6, 7, 2, 3, 8, 4, 5, 1]\n"
     ]
    }
   ],
   "source": [
    "list = [1, 2, 3, 4, 5, 6, 7, 8]\n",
    "print(new_shuffle(list))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.21\n",
    "\n",
    "不会，不知道在说什么。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.22"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "def point_mult(a, b):\n",
    "    c = []\n",
    "    for i in range(0, len(a)):\n",
    "        c.append(a[i]*b[i])\n",
    "    return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6, 14, 24, 36, 50]\n"
     ]
    }
   ],
   "source": [
    "a = [1, 2, 3, 4, 5]\n",
    "b = [6, 7, 8, 9, 10]\n",
    "print(point_mult(a, b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.23\n",
    "\n",
    "`try`和`except`的运用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_print(list):\n",
    "    for i in range(10):\n",
    "        try:\n",
    "            print(list[i])\n",
    "        except IndexError:\n",
    "            print('Don\\'t try buffer overflow attacks in Python!')\n",
    "            break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "Don't try buffer overflow attacks in Python!\n"
     ]
    }
   ],
   "source": [
    "list_print([1, 2, 3, 4, 5, 6, 7])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.24\n",
    "\n",
    "列表解析用处多多，从一个列表出发，生成另外一个列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_vowel(string):\n",
    "    return sum([i in 'aeiou' for i in string])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "print(calculate_vowel('abcdefg'))\n",
    "print(calculate_vowel('aeiouaeiou'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.25\n",
    "\n",
    "列表解析，加上`'分隔符'.join(list)`将列表转化为字符串；列表解析可以在一定条件下（如元素不是标点符号），从一个列表（也可以是字符串，只要可迭代就可以）生成一个列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "from string import punctuation\n",
    "def move_punctuation(string):\n",
    "    return ''.join([k for k in string if k not in punctuation])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lets go\n"
     ]
    }
   ],
   "source": [
    "a = 'Let\\'s go!'\n",
    "print(move_punctuation(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.26"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def input_abc():\n",
    "    a = float(input('请输入a的值：'))\n",
    "    b = float(input('请输入b的值：'))\n",
    "    c = float(input('请输入c的值：'))\n",
    "    print('a、b之和是否为c:', a+b == c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入a的值：3\n",
      "请输入b的值：3\n",
      "请输入c的值：6\n",
      "a、b之和是否为c: True\n"
     ]
    }
   ],
   "source": [
    "input_abc()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.27\n",
    "\n",
    "涉及生成器，先跳过。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C-1.28"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def norm(v, p = 2):\n",
    "    return (sum([k**p for k in v]))**(1/p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.7416573867739413\n",
      "3.3019272488946263\n"
     ]
    }
   ],
   "source": [
    "list = [1, 2, 3]\n",
    "print(norm(list))\n",
    "print(norm(list, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "P-1.29\n",
    "\n",
    "输出几个字母的所有可能排列组合。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "共720种排列\n",
      "catdog\n",
      "catdgo\n",
      "catodg\n",
      "catogd\n",
      "catgdo\n",
      "catgod\n",
      "cadtog\n",
      "cadtgo\n",
      "cadotg\n",
      "cadogt\n",
      "cadgto\n",
      "cadgot\n",
      "caotdg\n",
      "caotgd\n",
      "caodtg\n",
      "caodgt\n",
      "caogtd\n",
      "caogdt\n",
      "cagtdo\n",
      "cagtod\n",
      "cagdto\n",
      "cagdot\n",
      "cagotd\n",
      "cagodt\n",
      "ctadog\n",
      "ctadgo\n",
      "ctaodg\n",
      "ctaogd\n",
      "ctagdo\n",
      "ctagod\n",
      "ctdaog\n",
      "ctdago\n",
      "ctdoag\n",
      "ctdoga\n",
      "ctdgao\n",
      "ctdgoa\n",
      "ctoadg\n",
      "ctoagd\n",
      "ctodag\n",
      "ctodga\n",
      "ctogad\n",
      "ctogda\n",
      "ctgado\n",
      "ctgaod\n",
      "ctgdao\n",
      "ctgdoa\n",
      "ctgoad\n",
      "ctgoda\n",
      "cdatog\n",
      "cdatgo\n",
      "cdaotg\n",
      "cdaogt\n",
      "cdagto\n",
      "cdagot\n",
      "cdtaog\n",
      "cdtago\n",
      "cdtoag\n",
      "cdtoga\n",
      "cdtgao\n",
      "cdtgoa\n",
      "cdoatg\n",
      "cdoagt\n",
      "cdotag\n",
      "cdotga\n",
      "cdogat\n",
      "cdogta\n",
      "cdgato\n",
      "cdgaot\n",
      "cdgtao\n",
      "cdgtoa\n",
      "cdgoat\n",
      "cdgota\n",
      "coatdg\n",
      "coatgd\n",
      "coadtg\n",
      "coadgt\n",
      "coagtd\n",
      "coagdt\n",
      "cotadg\n",
      "cotagd\n",
      "cotdag\n",
      "cotdga\n",
      "cotgad\n",
      "cotgda\n",
      "codatg\n",
      "codagt\n",
      "codtag\n",
      "codtga\n",
      "codgat\n",
      "codgta\n",
      "cogatd\n",
      "cogadt\n",
      "cogtad\n",
      "cogtda\n",
      "cogdat\n",
      "cogdta\n",
      "cgatdo\n",
      "cgatod\n",
      "cgadto\n",
      "cgadot\n",
      "cgaotd\n",
      "cgaodt\n",
      "cgtado\n",
      "cgtaod\n",
      "cgtdao\n",
      "cgtdoa\n",
      "cgtoad\n",
      "cgtoda\n",
      "cgdato\n",
      "cgdaot\n",
      "cgdtao\n",
      "cgdtoa\n",
      "cgdoat\n",
      "cgdota\n",
      "cgoatd\n",
      "cgoadt\n",
      "cgotad\n",
      "cgotda\n",
      "cgodat\n",
      "cgodta\n",
      "actdog\n",
      "actdgo\n",
      "actodg\n",
      "actogd\n",
      "actgdo\n",
      "actgod\n",
      "acdtog\n",
      "acdtgo\n",
      "acdotg\n",
      "acdogt\n",
      "acdgto\n",
      "acdgot\n",
      "acotdg\n",
      "acotgd\n",
      "acodtg\n",
      "acodgt\n",
      "acogtd\n",
      "acogdt\n",
      "acgtdo\n",
      "acgtod\n",
      "acgdto\n",
      "acgdot\n",
      "acgotd\n",
      "acgodt\n",
      "atcdog\n",
      "atcdgo\n",
      "atcodg\n",
      "atcogd\n",
      "atcgdo\n",
      "atcgod\n",
      "atdcog\n",
      "atdcgo\n",
      "atdocg\n",
      "atdogc\n",
      "atdgco\n",
      "atdgoc\n",
      "atocdg\n",
      "atocgd\n",
      "atodcg\n",
      "atodgc\n",
      "atogcd\n",
      "atogdc\n",
      "atgcdo\n",
      "atgcod\n",
      "atgdco\n",
      "atgdoc\n",
      "atgocd\n",
      "atgodc\n",
      "adctog\n",
      "adctgo\n",
      "adcotg\n",
      "adcogt\n",
      "adcgto\n",
      "adcgot\n",
      "adtcog\n",
      "adtcgo\n",
      "adtocg\n",
      "adtogc\n",
      "adtgco\n",
      "adtgoc\n",
      "adoctg\n",
      "adocgt\n",
      "adotcg\n",
      "adotgc\n",
      "adogct\n",
      "adogtc\n",
      "adgcto\n",
      "adgcot\n",
      "adgtco\n",
      "adgtoc\n",
      "adgoct\n",
      "adgotc\n",
      "aoctdg\n",
      "aoctgd\n",
      "aocdtg\n",
      "aocdgt\n",
      "aocgtd\n",
      "aocgdt\n",
      "aotcdg\n",
      "aotcgd\n",
      "aotdcg\n",
      "aotdgc\n",
      "aotgcd\n",
      "aotgdc\n",
      "aodctg\n",
      "aodcgt\n",
      "aodtcg\n",
      "aodtgc\n",
      "aodgct\n",
      "aodgtc\n",
      "aogctd\n",
      "aogcdt\n",
      "aogtcd\n",
      "aogtdc\n",
      "aogdct\n",
      "aogdtc\n",
      "agctdo\n",
      "agctod\n",
      "agcdto\n",
      "agcdot\n",
      "agcotd\n",
      "agcodt\n",
      "agtcdo\n",
      "agtcod\n",
      "agtdco\n",
      "agtdoc\n",
      "agtocd\n",
      "agtodc\n",
      "agdcto\n",
      "agdcot\n",
      "agdtco\n",
      "agdtoc\n",
      "agdoct\n",
      "agdotc\n",
      "agoctd\n",
      "agocdt\n",
      "agotcd\n",
      "agotdc\n",
      "agodct\n",
      "agodtc\n",
      "tcadog\n",
      "tcadgo\n",
      "tcaodg\n",
      "tcaogd\n",
      "tcagdo\n",
      "tcagod\n",
      "tcdaog\n",
      "tcdago\n",
      "tcdoag\n",
      "tcdoga\n",
      "tcdgao\n",
      "tcdgoa\n",
      "tcoadg\n",
      "tcoagd\n",
      "tcodag\n",
      "tcodga\n",
      "tcogad\n",
      "tcogda\n",
      "tcgado\n",
      "tcgaod\n",
      "tcgdao\n",
      "tcgdoa\n",
      "tcgoad\n",
      "tcgoda\n",
      "tacdog\n",
      "tacdgo\n",
      "tacodg\n",
      "tacogd\n",
      "tacgdo\n",
      "tacgod\n",
      "tadcog\n",
      "tadcgo\n",
      "tadocg\n",
      "tadogc\n",
      "tadgco\n",
      "tadgoc\n",
      "taocdg\n",
      "taocgd\n",
      "taodcg\n",
      "taodgc\n",
      "taogcd\n",
      "taogdc\n",
      "tagcdo\n",
      "tagcod\n",
      "tagdco\n",
      "tagdoc\n",
      "tagocd\n",
      "tagodc\n",
      "tdcaog\n",
      "tdcago\n",
      "tdcoag\n",
      "tdcoga\n",
      "tdcgao\n",
      "tdcgoa\n",
      "tdacog\n",
      "tdacgo\n",
      "tdaocg\n",
      "tdaogc\n",
      "tdagco\n",
      "tdagoc\n",
      "tdocag\n",
      "tdocga\n",
      "tdoacg\n",
      "tdoagc\n",
      "tdogca\n",
      "tdogac\n",
      "tdgcao\n",
      "tdgcoa\n",
      "tdgaco\n",
      "tdgaoc\n",
      "tdgoca\n",
      "tdgoac\n",
      "tocadg\n",
      "tocagd\n",
      "tocdag\n",
      "tocdga\n",
      "tocgad\n",
      "tocgda\n",
      "toacdg\n",
      "toacgd\n",
      "toadcg\n",
      "toadgc\n",
      "toagcd\n",
      "toagdc\n",
      "todcag\n",
      "todcga\n",
      "todacg\n",
      "todagc\n",
      "todgca\n",
      "todgac\n",
      "togcad\n",
      "togcda\n",
      "togacd\n",
      "togadc\n",
      "togdca\n",
      "togdac\n",
      "tgcado\n",
      "tgcaod\n",
      "tgcdao\n",
      "tgcdoa\n",
      "tgcoad\n",
      "tgcoda\n",
      "tgacdo\n",
      "tgacod\n",
      "tgadco\n",
      "tgadoc\n",
      "tgaocd\n",
      "tgaodc\n",
      "tgdcao\n",
      "tgdcoa\n",
      "tgdaco\n",
      "tgdaoc\n",
      "tgdoca\n",
      "tgdoac\n",
      "tgocad\n",
      "tgocda\n",
      "tgoacd\n",
      "tgoadc\n",
      "tgodca\n",
      "tgodac\n",
      "dcatog\n",
      "dcatgo\n",
      "dcaotg\n",
      "dcaogt\n",
      "dcagto\n",
      "dcagot\n",
      "dctaog\n",
      "dctago\n",
      "dctoag\n",
      "dctoga\n",
      "dctgao\n",
      "dctgoa\n",
      "dcoatg\n",
      "dcoagt\n",
      "dcotag\n",
      "dcotga\n",
      "dcogat\n",
      "dcogta\n",
      "dcgato\n",
      "dcgaot\n",
      "dcgtao\n",
      "dcgtoa\n",
      "dcgoat\n",
      "dcgota\n",
      "dactog\n",
      "dactgo\n",
      "dacotg\n",
      "dacogt\n",
      "dacgto\n",
      "dacgot\n",
      "datcog\n",
      "datcgo\n",
      "datocg\n",
      "datogc\n",
      "datgco\n",
      "datgoc\n",
      "daoctg\n",
      "daocgt\n",
      "daotcg\n",
      "daotgc\n",
      "daogct\n",
      "daogtc\n",
      "dagcto\n",
      "dagcot\n",
      "dagtco\n",
      "dagtoc\n",
      "dagoct\n",
      "dagotc\n",
      "dtcaog\n",
      "dtcago\n",
      "dtcoag\n",
      "dtcoga\n",
      "dtcgao\n",
      "dtcgoa\n",
      "dtacog\n",
      "dtacgo\n",
      "dtaocg\n",
      "dtaogc\n",
      "dtagco\n",
      "dtagoc\n",
      "dtocag\n",
      "dtocga\n",
      "dtoacg\n",
      "dtoagc\n",
      "dtogca\n",
      "dtogac\n",
      "dtgcao\n",
      "dtgcoa\n",
      "dtgaco\n",
      "dtgaoc\n",
      "dtgoca\n",
      "dtgoac\n",
      "docatg\n",
      "docagt\n",
      "doctag\n",
      "doctga\n",
      "docgat\n",
      "docgta\n",
      "doactg\n",
      "doacgt\n",
      "doatcg\n",
      "doatgc\n",
      "doagct\n",
      "doagtc\n",
      "dotcag\n",
      "dotcga\n",
      "dotacg\n",
      "dotagc\n",
      "dotgca\n",
      "dotgac\n",
      "dogcat\n",
      "dogcta\n",
      "dogact\n",
      "dogatc\n",
      "dogtca\n",
      "dogtac\n",
      "dgcato\n",
      "dgcaot\n",
      "dgctao\n",
      "dgctoa\n",
      "dgcoat\n",
      "dgcota\n",
      "dgacto\n",
      "dgacot\n",
      "dgatco\n",
      "dgatoc\n",
      "dgaoct\n",
      "dgaotc\n",
      "dgtcao\n",
      "dgtcoa\n",
      "dgtaco\n",
      "dgtaoc\n",
      "dgtoca\n",
      "dgtoac\n",
      "dgocat\n",
      "dgocta\n",
      "dgoact\n",
      "dgoatc\n",
      "dgotca\n",
      "dgotac\n",
      "ocatdg\n",
      "ocatgd\n",
      "ocadtg\n",
      "ocadgt\n",
      "ocagtd\n",
      "ocagdt\n",
      "octadg\n",
      "octagd\n",
      "octdag\n",
      "octdga\n",
      "octgad\n",
      "octgda\n",
      "ocdatg\n",
      "ocdagt\n",
      "ocdtag\n",
      "ocdtga\n",
      "ocdgat\n",
      "ocdgta\n",
      "ocgatd\n",
      "ocgadt\n",
      "ocgtad\n",
      "ocgtda\n",
      "ocgdat\n",
      "ocgdta\n",
      "oactdg\n",
      "oactgd\n",
      "oacdtg\n",
      "oacdgt\n",
      "oacgtd\n",
      "oacgdt\n",
      "oatcdg\n",
      "oatcgd\n",
      "oatdcg\n",
      "oatdgc\n",
      "oatgcd\n",
      "oatgdc\n",
      "oadctg\n",
      "oadcgt\n",
      "oadtcg\n",
      "oadtgc\n",
      "oadgct\n",
      "oadgtc\n",
      "oagctd\n",
      "oagcdt\n",
      "oagtcd\n",
      "oagtdc\n",
      "oagdct\n",
      "oagdtc\n",
      "otcadg\n",
      "otcagd\n",
      "otcdag\n",
      "otcdga\n",
      "otcgad\n",
      "otcgda\n",
      "otacdg\n",
      "otacgd\n",
      "otadcg\n",
      "otadgc\n",
      "otagcd\n",
      "otagdc\n",
      "otdcag\n",
      "otdcga\n",
      "otdacg\n",
      "otdagc\n",
      "otdgca\n",
      "otdgac\n",
      "otgcad\n",
      "otgcda\n",
      "otgacd\n",
      "otgadc\n",
      "otgdca\n",
      "otgdac\n",
      "odcatg\n",
      "odcagt\n",
      "odctag\n",
      "odctga\n",
      "odcgat\n",
      "odcgta\n",
      "odactg\n",
      "odacgt\n",
      "odatcg\n",
      "odatgc\n",
      "odagct\n",
      "odagtc\n",
      "odtcag\n",
      "odtcga\n",
      "odtacg\n",
      "odtagc\n",
      "odtgca\n",
      "odtgac\n",
      "odgcat\n",
      "odgcta\n",
      "odgact\n",
      "odgatc\n",
      "odgtca\n",
      "odgtac\n",
      "ogcatd\n",
      "ogcadt\n",
      "ogctad\n",
      "ogctda\n",
      "ogcdat\n",
      "ogcdta\n",
      "ogactd\n",
      "ogacdt\n",
      "ogatcd\n",
      "ogatdc\n",
      "ogadct\n",
      "ogadtc\n",
      "ogtcad\n",
      "ogtcda\n",
      "ogtacd\n",
      "ogtadc\n",
      "ogtdca\n",
      "ogtdac\n",
      "ogdcat\n",
      "ogdcta\n",
      "ogdact\n",
      "ogdatc\n",
      "ogdtca\n",
      "ogdtac\n",
      "gcatdo\n",
      "gcatod\n",
      "gcadto\n",
      "gcadot\n",
      "gcaotd\n",
      "gcaodt\n",
      "gctado\n",
      "gctaod\n",
      "gctdao\n",
      "gctdoa\n",
      "gctoad\n",
      "gctoda\n",
      "gcdato\n",
      "gcdaot\n",
      "gcdtao\n",
      "gcdtoa\n",
      "gcdoat\n",
      "gcdota\n",
      "gcoatd\n",
      "gcoadt\n",
      "gcotad\n",
      "gcotda\n",
      "gcodat\n",
      "gcodta\n",
      "gactdo\n",
      "gactod\n",
      "gacdto\n",
      "gacdot\n",
      "gacotd\n",
      "gacodt\n",
      "gatcdo\n",
      "gatcod\n",
      "gatdco\n",
      "gatdoc\n",
      "gatocd\n",
      "gatodc\n",
      "gadcto\n",
      "gadcot\n",
      "gadtco\n",
      "gadtoc\n",
      "gadoct\n",
      "gadotc\n",
      "gaoctd\n",
      "gaocdt\n",
      "gaotcd\n",
      "gaotdc\n",
      "gaodct\n",
      "gaodtc\n",
      "gtcado\n",
      "gtcaod\n",
      "gtcdao\n",
      "gtcdoa\n",
      "gtcoad\n",
      "gtcoda\n",
      "gtacdo\n",
      "gtacod\n",
      "gtadco\n",
      "gtadoc\n",
      "gtaocd\n",
      "gtaodc\n",
      "gtdcao\n",
      "gtdcoa\n",
      "gtdaco\n",
      "gtdaoc\n",
      "gtdoca\n",
      "gtdoac\n",
      "gtocad\n",
      "gtocda\n",
      "gtoacd\n",
      "gtoadc\n",
      "gtodca\n",
      "gtodac\n",
      "gdcato\n",
      "gdcaot\n",
      "gdctao\n",
      "gdctoa\n",
      "gdcoat\n",
      "gdcota\n",
      "gdacto\n",
      "gdacot\n",
      "gdatco\n",
      "gdatoc\n",
      "gdaoct\n",
      "gdaotc\n",
      "gdtcao\n",
      "gdtcoa\n",
      "gdtaco\n",
      "gdtaoc\n",
      "gdtoca\n",
      "gdtoac\n",
      "gdocat\n",
      "gdocta\n",
      "gdoact\n",
      "gdoatc\n",
      "gdotca\n",
      "gdotac\n",
      "gocatd\n",
      "gocadt\n",
      "goctad\n",
      "goctda\n",
      "gocdat\n",
      "gocdta\n",
      "goactd\n",
      "goacdt\n",
      "goatcd\n",
      "goatdc\n",
      "goadct\n",
      "goadtc\n",
      "gotcad\n",
      "gotcda\n",
      "gotacd\n",
      "gotadc\n",
      "gotdca\n",
      "gotdac\n",
      "godcat\n",
      "godcta\n",
      "godact\n",
      "godatc\n",
      "godtca\n",
      "godtac\n"
     ]
    }
   ],
   "source": [
    "list = ['c', 'a', 't', 'd', 'o', 'g']\n",
    "string = []\n",
    "for i in list:\n",
    "    for j in list:\n",
    "        for z in list:\n",
    "            for x in list:\n",
    "                for y in list:\n",
    "                    for t in list:\n",
    "                        if len(set([i, j, z, x, y, t])) == 6:\n",
    "                            string.append([i, j, z, x, y, t])\n",
    "print('共' + str(len(string)) + '种排列')\n",
    "for strings in string:\n",
    "    print(''.join(strings))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面的是先将重复的也遍历一遍而不是防止重复，如果要防止重复，就需要牺牲空间，即建立一个新的list存储剩下的元素。最终字符串长度为几，就用几层循环，这是一种直接枚举的方法，下面用防止重复的方法也做一遍。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "共720种排列\n",
      "catdog\n",
      "catdgo\n",
      "catodg\n",
      "catogd\n",
      "catgdo\n",
      "catgod\n",
      "cadtog\n",
      "cadtgo\n",
      "cadotg\n",
      "cadogt\n",
      "cadgto\n",
      "cadgot\n",
      "caotdg\n",
      "caotgd\n",
      "caodtg\n",
      "caodgt\n",
      "caogtd\n",
      "caogdt\n",
      "cagtdo\n",
      "cagtod\n",
      "cagdto\n",
      "cagdot\n",
      "cagotd\n",
      "cagodt\n",
      "ctadog\n",
      "ctadgo\n",
      "ctaodg\n",
      "ctaogd\n",
      "ctagdo\n",
      "ctagod\n",
      "ctdaog\n",
      "ctdago\n",
      "ctdoag\n",
      "ctdoga\n",
      "ctdgao\n",
      "ctdgoa\n",
      "ctoadg\n",
      "ctoagd\n",
      "ctodag\n",
      "ctodga\n",
      "ctogad\n",
      "ctogda\n",
      "ctgado\n",
      "ctgaod\n",
      "ctgdao\n",
      "ctgdoa\n",
      "ctgoad\n",
      "ctgoda\n",
      "cdatog\n",
      "cdatgo\n",
      "cdaotg\n",
      "cdaogt\n",
      "cdagto\n",
      "cdagot\n",
      "cdtaog\n",
      "cdtago\n",
      "cdtoag\n",
      "cdtoga\n",
      "cdtgao\n",
      "cdtgoa\n",
      "cdoatg\n",
      "cdoagt\n",
      "cdotag\n",
      "cdotga\n",
      "cdogat\n",
      "cdogta\n",
      "cdgato\n",
      "cdgaot\n",
      "cdgtao\n",
      "cdgtoa\n",
      "cdgoat\n",
      "cdgota\n",
      "coatdg\n",
      "coatgd\n",
      "coadtg\n",
      "coadgt\n",
      "coagtd\n",
      "coagdt\n",
      "cotadg\n",
      "cotagd\n",
      "cotdag\n",
      "cotdga\n",
      "cotgad\n",
      "cotgda\n",
      "codatg\n",
      "codagt\n",
      "codtag\n",
      "codtga\n",
      "codgat\n",
      "codgta\n",
      "cogatd\n",
      "cogadt\n",
      "cogtad\n",
      "cogtda\n",
      "cogdat\n",
      "cogdta\n",
      "cgatdo\n",
      "cgatod\n",
      "cgadto\n",
      "cgadot\n",
      "cgaotd\n",
      "cgaodt\n",
      "cgtado\n",
      "cgtaod\n",
      "cgtdao\n",
      "cgtdoa\n",
      "cgtoad\n",
      "cgtoda\n",
      "cgdato\n",
      "cgdaot\n",
      "cgdtao\n",
      "cgdtoa\n",
      "cgdoat\n",
      "cgdota\n",
      "cgoatd\n",
      "cgoadt\n",
      "cgotad\n",
      "cgotda\n",
      "cgodat\n",
      "cgodta\n",
      "actdog\n",
      "actdgo\n",
      "actodg\n",
      "actogd\n",
      "actgdo\n",
      "actgod\n",
      "acdtog\n",
      "acdtgo\n",
      "acdotg\n",
      "acdogt\n",
      "acdgto\n",
      "acdgot\n",
      "acotdg\n",
      "acotgd\n",
      "acodtg\n",
      "acodgt\n",
      "acogtd\n",
      "acogdt\n",
      "acgtdo\n",
      "acgtod\n",
      "acgdto\n",
      "acgdot\n",
      "acgotd\n",
      "acgodt\n",
      "atcdog\n",
      "atcdgo\n",
      "atcodg\n",
      "atcogd\n",
      "atcgdo\n",
      "atcgod\n",
      "atdcog\n",
      "atdcgo\n",
      "atdocg\n",
      "atdogc\n",
      "atdgco\n",
      "atdgoc\n",
      "atocdg\n",
      "atocgd\n",
      "atodcg\n",
      "atodgc\n",
      "atogcd\n",
      "atogdc\n",
      "atgcdo\n",
      "atgcod\n",
      "atgdco\n",
      "atgdoc\n",
      "atgocd\n",
      "atgodc\n",
      "adctog\n",
      "adctgo\n",
      "adcotg\n",
      "adcogt\n",
      "adcgto\n",
      "adcgot\n",
      "adtcog\n",
      "adtcgo\n",
      "adtocg\n",
      "adtogc\n",
      "adtgco\n",
      "adtgoc\n",
      "adoctg\n",
      "adocgt\n",
      "adotcg\n",
      "adotgc\n",
      "adogct\n",
      "adogtc\n",
      "adgcto\n",
      "adgcot\n",
      "adgtco\n",
      "adgtoc\n",
      "adgoct\n",
      "adgotc\n",
      "aoctdg\n",
      "aoctgd\n",
      "aocdtg\n",
      "aocdgt\n",
      "aocgtd\n",
      "aocgdt\n",
      "aotcdg\n",
      "aotcgd\n",
      "aotdcg\n",
      "aotdgc\n",
      "aotgcd\n",
      "aotgdc\n",
      "aodctg\n",
      "aodcgt\n",
      "aodtcg\n",
      "aodtgc\n",
      "aodgct\n",
      "aodgtc\n",
      "aogctd\n",
      "aogcdt\n",
      "aogtcd\n",
      "aogtdc\n",
      "aogdct\n",
      "aogdtc\n",
      "agctdo\n",
      "agctod\n",
      "agcdto\n",
      "agcdot\n",
      "agcotd\n",
      "agcodt\n",
      "agtcdo\n",
      "agtcod\n",
      "agtdco\n",
      "agtdoc\n",
      "agtocd\n",
      "agtodc\n",
      "agdcto\n",
      "agdcot\n",
      "agdtco\n",
      "agdtoc\n",
      "agdoct\n",
      "agdotc\n",
      "agoctd\n",
      "agocdt\n",
      "agotcd\n",
      "agotdc\n",
      "agodct\n",
      "agodtc\n",
      "tcadog\n",
      "tcadgo\n",
      "tcaodg\n",
      "tcaogd\n",
      "tcagdo\n",
      "tcagod\n",
      "tcdaog\n",
      "tcdago\n",
      "tcdoag\n",
      "tcdoga\n",
      "tcdgao\n",
      "tcdgoa\n",
      "tcoadg\n",
      "tcoagd\n",
      "tcodag\n",
      "tcodga\n",
      "tcogad\n",
      "tcogda\n",
      "tcgado\n",
      "tcgaod\n",
      "tcgdao\n",
      "tcgdoa\n",
      "tcgoad\n",
      "tcgoda\n",
      "tacdog\n",
      "tacdgo\n",
      "tacodg\n",
      "tacogd\n",
      "tacgdo\n",
      "tacgod\n",
      "tadcog\n",
      "tadcgo\n",
      "tadocg\n",
      "tadogc\n",
      "tadgco\n",
      "tadgoc\n",
      "taocdg\n",
      "taocgd\n",
      "taodcg\n",
      "taodgc\n",
      "taogcd\n",
      "taogdc\n",
      "tagcdo\n",
      "tagcod\n",
      "tagdco\n",
      "tagdoc\n",
      "tagocd\n",
      "tagodc\n",
      "tdcaog\n",
      "tdcago\n",
      "tdcoag\n",
      "tdcoga\n",
      "tdcgao\n",
      "tdcgoa\n",
      "tdacog\n",
      "tdacgo\n",
      "tdaocg\n",
      "tdaogc\n",
      "tdagco\n",
      "tdagoc\n",
      "tdocag\n",
      "tdocga\n",
      "tdoacg\n",
      "tdoagc\n",
      "tdogca\n",
      "tdogac\n",
      "tdgcao\n",
      "tdgcoa\n",
      "tdgaco\n",
      "tdgaoc\n",
      "tdgoca\n",
      "tdgoac\n",
      "tocadg\n",
      "tocagd\n",
      "tocdag\n",
      "tocdga\n",
      "tocgad\n",
      "tocgda\n",
      "toacdg\n",
      "toacgd\n",
      "toadcg\n",
      "toadgc\n",
      "toagcd\n",
      "toagdc\n",
      "todcag\n",
      "todcga\n",
      "todacg\n",
      "todagc\n",
      "todgca\n",
      "todgac\n",
      "togcad\n",
      "togcda\n",
      "togacd\n",
      "togadc\n",
      "togdca\n",
      "togdac\n",
      "tgcado\n",
      "tgcaod\n",
      "tgcdao\n",
      "tgcdoa\n",
      "tgcoad\n",
      "tgcoda\n",
      "tgacdo\n",
      "tgacod\n",
      "tgadco\n",
      "tgadoc\n",
      "tgaocd\n",
      "tgaodc\n",
      "tgdcao\n",
      "tgdcoa\n",
      "tgdaco\n",
      "tgdaoc\n",
      "tgdoca\n",
      "tgdoac\n",
      "tgocad\n",
      "tgocda\n",
      "tgoacd\n",
      "tgoadc\n",
      "tgodca\n",
      "tgodac\n",
      "dcatog\n",
      "dcatgo\n",
      "dcaotg\n",
      "dcaogt\n",
      "dcagto\n",
      "dcagot\n",
      "dctaog\n",
      "dctago\n",
      "dctoag\n",
      "dctoga\n",
      "dctgao\n",
      "dctgoa\n",
      "dcoatg\n",
      "dcoagt\n",
      "dcotag\n",
      "dcotga\n",
      "dcogat\n",
      "dcogta\n",
      "dcgato\n",
      "dcgaot\n",
      "dcgtao\n",
      "dcgtoa\n",
      "dcgoat\n",
      "dcgota\n",
      "dactog\n",
      "dactgo\n",
      "dacotg\n",
      "dacogt\n",
      "dacgto\n",
      "dacgot\n",
      "datcog\n",
      "datcgo\n",
      "datocg\n",
      "datogc\n",
      "datgco\n",
      "datgoc\n",
      "daoctg\n",
      "daocgt\n",
      "daotcg\n",
      "daotgc\n",
      "daogct\n",
      "daogtc\n",
      "dagcto\n",
      "dagcot\n",
      "dagtco\n",
      "dagtoc\n",
      "dagoct\n",
      "dagotc\n",
      "dtcaog\n",
      "dtcago\n",
      "dtcoag\n",
      "dtcoga\n",
      "dtcgao\n",
      "dtcgoa\n",
      "dtacog\n",
      "dtacgo\n",
      "dtaocg\n",
      "dtaogc\n",
      "dtagco\n",
      "dtagoc\n",
      "dtocag\n",
      "dtocga\n",
      "dtoacg\n",
      "dtoagc\n",
      "dtogca\n",
      "dtogac\n",
      "dtgcao\n",
      "dtgcoa\n",
      "dtgaco\n",
      "dtgaoc\n",
      "dtgoca\n",
      "dtgoac\n",
      "docatg\n",
      "docagt\n",
      "doctag\n",
      "doctga\n",
      "docgat\n",
      "docgta\n",
      "doactg\n",
      "doacgt\n",
      "doatcg\n",
      "doatgc\n",
      "doagct\n",
      "doagtc\n",
      "dotcag\n",
      "dotcga\n",
      "dotacg\n",
      "dotagc\n",
      "dotgca\n",
      "dotgac\n",
      "dogcat\n",
      "dogcta\n",
      "dogact\n",
      "dogatc\n",
      "dogtca\n",
      "dogtac\n",
      "dgcato\n",
      "dgcaot\n",
      "dgctao\n",
      "dgctoa\n",
      "dgcoat\n",
      "dgcota\n",
      "dgacto\n",
      "dgacot\n",
      "dgatco\n",
      "dgatoc\n",
      "dgaoct\n",
      "dgaotc\n",
      "dgtcao\n",
      "dgtcoa\n",
      "dgtaco\n",
      "dgtaoc\n",
      "dgtoca\n",
      "dgtoac\n",
      "dgocat\n",
      "dgocta\n",
      "dgoact\n",
      "dgoatc\n",
      "dgotca\n",
      "dgotac\n",
      "ocatdg\n",
      "ocatgd\n",
      "ocadtg\n",
      "ocadgt\n",
      "ocagtd\n",
      "ocagdt\n",
      "octadg\n",
      "octagd\n",
      "octdag\n",
      "octdga\n",
      "octgad\n",
      "octgda\n",
      "ocdatg\n",
      "ocdagt\n",
      "ocdtag\n",
      "ocdtga\n",
      "ocdgat\n",
      "ocdgta\n",
      "ocgatd\n",
      "ocgadt\n",
      "ocgtad\n",
      "ocgtda\n",
      "ocgdat\n",
      "ocgdta\n",
      "oactdg\n",
      "oactgd\n",
      "oacdtg\n",
      "oacdgt\n",
      "oacgtd\n",
      "oacgdt\n",
      "oatcdg\n",
      "oatcgd\n",
      "oatdcg\n",
      "oatdgc\n",
      "oatgcd\n",
      "oatgdc\n",
      "oadctg\n",
      "oadcgt\n",
      "oadtcg\n",
      "oadtgc\n",
      "oadgct\n",
      "oadgtc\n",
      "oagctd\n",
      "oagcdt\n",
      "oagtcd\n",
      "oagtdc\n",
      "oagdct\n",
      "oagdtc\n",
      "otcadg\n",
      "otcagd\n",
      "otcdag\n",
      "otcdga\n",
      "otcgad\n",
      "otcgda\n",
      "otacdg\n",
      "otacgd\n",
      "otadcg\n",
      "otadgc\n",
      "otagcd\n",
      "otagdc\n",
      "otdcag\n",
      "otdcga\n",
      "otdacg\n",
      "otdagc\n",
      "otdgca\n",
      "otdgac\n",
      "otgcad\n",
      "otgcda\n",
      "otgacd\n",
      "otgadc\n",
      "otgdca\n",
      "otgdac\n",
      "odcatg\n",
      "odcagt\n",
      "odctag\n",
      "odctga\n",
      "odcgat\n",
      "odcgta\n",
      "odactg\n",
      "odacgt\n",
      "odatcg\n",
      "odatgc\n",
      "odagct\n",
      "odagtc\n",
      "odtcag\n",
      "odtcga\n",
      "odtacg\n",
      "odtagc\n",
      "odtgca\n",
      "odtgac\n",
      "odgcat\n",
      "odgcta\n",
      "odgact\n",
      "odgatc\n",
      "odgtca\n",
      "odgtac\n",
      "ogcatd\n",
      "ogcadt\n",
      "ogctad\n",
      "ogctda\n",
      "ogcdat\n",
      "ogcdta\n",
      "ogactd\n",
      "ogacdt\n",
      "ogatcd\n",
      "ogatdc\n",
      "ogadct\n",
      "ogadtc\n",
      "ogtcad\n",
      "ogtcda\n",
      "ogtacd\n",
      "ogtadc\n",
      "ogtdca\n",
      "ogtdac\n",
      "ogdcat\n",
      "ogdcta\n",
      "ogdact\n",
      "ogdatc\n",
      "ogdtca\n",
      "ogdtac\n",
      "gcatdo\n",
      "gcatod\n",
      "gcadto\n",
      "gcadot\n",
      "gcaotd\n",
      "gcaodt\n",
      "gctado\n",
      "gctaod\n",
      "gctdao\n",
      "gctdoa\n",
      "gctoad\n",
      "gctoda\n",
      "gcdato\n",
      "gcdaot\n",
      "gcdtao\n",
      "gcdtoa\n",
      "gcdoat\n",
      "gcdota\n",
      "gcoatd\n",
      "gcoadt\n",
      "gcotad\n",
      "gcotda\n",
      "gcodat\n",
      "gcodta\n",
      "gactdo\n",
      "gactod\n",
      "gacdto\n",
      "gacdot\n",
      "gacotd\n",
      "gacodt\n",
      "gatcdo\n",
      "gatcod\n",
      "gatdco\n",
      "gatdoc\n",
      "gatocd\n",
      "gatodc\n",
      "gadcto\n",
      "gadcot\n",
      "gadtco\n",
      "gadtoc\n",
      "gadoct\n",
      "gadotc\n",
      "gaoctd\n",
      "gaocdt\n",
      "gaotcd\n",
      "gaotdc\n",
      "gaodct\n",
      "gaodtc\n",
      "gtcado\n",
      "gtcaod\n",
      "gtcdao\n",
      "gtcdoa\n",
      "gtcoad\n",
      "gtcoda\n",
      "gtacdo\n",
      "gtacod\n",
      "gtadco\n",
      "gtadoc\n",
      "gtaocd\n",
      "gtaodc\n",
      "gtdcao\n",
      "gtdcoa\n",
      "gtdaco\n",
      "gtdaoc\n",
      "gtdoca\n",
      "gtdoac\n",
      "gtocad\n",
      "gtocda\n",
      "gtoacd\n",
      "gtoadc\n",
      "gtodca\n",
      "gtodac\n",
      "gdcato\n",
      "gdcaot\n",
      "gdctao\n",
      "gdctoa\n",
      "gdcoat\n",
      "gdcota\n",
      "gdacto\n",
      "gdacot\n",
      "gdatco\n",
      "gdatoc\n",
      "gdaoct\n",
      "gdaotc\n",
      "gdtcao\n",
      "gdtcoa\n",
      "gdtaco\n",
      "gdtaoc\n",
      "gdtoca\n",
      "gdtoac\n",
      "gdocat\n",
      "gdocta\n",
      "gdoact\n",
      "gdoatc\n",
      "gdotca\n",
      "gdotac\n",
      "gocatd\n",
      "gocadt\n",
      "goctad\n",
      "goctda\n",
      "gocdat\n",
      "gocdta\n",
      "goactd\n",
      "goacdt\n",
      "goatcd\n",
      "goatdc\n",
      "goadct\n",
      "goadtc\n",
      "gotcad\n",
      "gotcda\n",
      "gotacd\n",
      "gotadc\n",
      "gotdca\n",
      "gotdac\n",
      "godcat\n",
      "godcta\n",
      "godact\n",
      "godatc\n",
      "godtca\n",
      "godtac\n"
     ]
    }
   ],
   "source": [
    "string = []\n",
    "for i in range(0, 6):\n",
    "    list_1 = list[ :i] + list[(i+1): ]\n",
    "    for j in range(0, 5):\n",
    "        list_2 = list_1[ :j] + list_1[(j+1): ]\n",
    "        for z in range(0, 4):\n",
    "            list_3 = list_2[ :z] + list_2[(z+1): ]\n",
    "            for x in range(0, 3):\n",
    "                list_4 = list_3[ :x] + list_3[(x+1): ]\n",
    "                string.append([list[i], list_1[j], list_2[z], list_3[x]] + list_4)\n",
    "                string.append([list[i], list_1[j], list_2[z], list_3[x]] + list_4[::-1])\n",
    "print('共' + str(len(string)) + '种排列')\n",
    "for strings in string:\n",
    "    print(''.join(strings))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最终选取的字符个数由循环层数决定。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面再用`itertools`模块。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "共720种排列\n",
      "catdog\n",
      "catdgo\n",
      "catodg\n",
      "catogd\n",
      "catgdo\n",
      "catgod\n",
      "cadtog\n",
      "cadtgo\n",
      "cadotg\n",
      "cadogt\n",
      "cadgto\n",
      "cadgot\n",
      "caotdg\n",
      "caotgd\n",
      "caodtg\n",
      "caodgt\n",
      "caogtd\n",
      "caogdt\n",
      "cagtdo\n",
      "cagtod\n",
      "cagdto\n",
      "cagdot\n",
      "cagotd\n",
      "cagodt\n",
      "ctadog\n",
      "ctadgo\n",
      "ctaodg\n",
      "ctaogd\n",
      "ctagdo\n",
      "ctagod\n",
      "ctdaog\n",
      "ctdago\n",
      "ctdoag\n",
      "ctdoga\n",
      "ctdgao\n",
      "ctdgoa\n",
      "ctoadg\n",
      "ctoagd\n",
      "ctodag\n",
      "ctodga\n",
      "ctogad\n",
      "ctogda\n",
      "ctgado\n",
      "ctgaod\n",
      "ctgdao\n",
      "ctgdoa\n",
      "ctgoad\n",
      "ctgoda\n",
      "cdatog\n",
      "cdatgo\n",
      "cdaotg\n",
      "cdaogt\n",
      "cdagto\n",
      "cdagot\n",
      "cdtaog\n",
      "cdtago\n",
      "cdtoag\n",
      "cdtoga\n",
      "cdtgao\n",
      "cdtgoa\n",
      "cdoatg\n",
      "cdoagt\n",
      "cdotag\n",
      "cdotga\n",
      "cdogat\n",
      "cdogta\n",
      "cdgato\n",
      "cdgaot\n",
      "cdgtao\n",
      "cdgtoa\n",
      "cdgoat\n",
      "cdgota\n",
      "coatdg\n",
      "coatgd\n",
      "coadtg\n",
      "coadgt\n",
      "coagtd\n",
      "coagdt\n",
      "cotadg\n",
      "cotagd\n",
      "cotdag\n",
      "cotdga\n",
      "cotgad\n",
      "cotgda\n",
      "codatg\n",
      "codagt\n",
      "codtag\n",
      "codtga\n",
      "codgat\n",
      "codgta\n",
      "cogatd\n",
      "cogadt\n",
      "cogtad\n",
      "cogtda\n",
      "cogdat\n",
      "cogdta\n",
      "cgatdo\n",
      "cgatod\n",
      "cgadto\n",
      "cgadot\n",
      "cgaotd\n",
      "cgaodt\n",
      "cgtado\n",
      "cgtaod\n",
      "cgtdao\n",
      "cgtdoa\n",
      "cgtoad\n",
      "cgtoda\n",
      "cgdato\n",
      "cgdaot\n",
      "cgdtao\n",
      "cgdtoa\n",
      "cgdoat\n",
      "cgdota\n",
      "cgoatd\n",
      "cgoadt\n",
      "cgotad\n",
      "cgotda\n",
      "cgodat\n",
      "cgodta\n",
      "actdog\n",
      "actdgo\n",
      "actodg\n",
      "actogd\n",
      "actgdo\n",
      "actgod\n",
      "acdtog\n",
      "acdtgo\n",
      "acdotg\n",
      "acdogt\n",
      "acdgto\n",
      "acdgot\n",
      "acotdg\n",
      "acotgd\n",
      "acodtg\n",
      "acodgt\n",
      "acogtd\n",
      "acogdt\n",
      "acgtdo\n",
      "acgtod\n",
      "acgdto\n",
      "acgdot\n",
      "acgotd\n",
      "acgodt\n",
      "atcdog\n",
      "atcdgo\n",
      "atcodg\n",
      "atcogd\n",
      "atcgdo\n",
      "atcgod\n",
      "atdcog\n",
      "atdcgo\n",
      "atdocg\n",
      "atdogc\n",
      "atdgco\n",
      "atdgoc\n",
      "atocdg\n",
      "atocgd\n",
      "atodcg\n",
      "atodgc\n",
      "atogcd\n",
      "atogdc\n",
      "atgcdo\n",
      "atgcod\n",
      "atgdco\n",
      "atgdoc\n",
      "atgocd\n",
      "atgodc\n",
      "adctog\n",
      "adctgo\n",
      "adcotg\n",
      "adcogt\n",
      "adcgto\n",
      "adcgot\n",
      "adtcog\n",
      "adtcgo\n",
      "adtocg\n",
      "adtogc\n",
      "adtgco\n",
      "adtgoc\n",
      "adoctg\n",
      "adocgt\n",
      "adotcg\n",
      "adotgc\n",
      "adogct\n",
      "adogtc\n",
      "adgcto\n",
      "adgcot\n",
      "adgtco\n",
      "adgtoc\n",
      "adgoct\n",
      "adgotc\n",
      "aoctdg\n",
      "aoctgd\n",
      "aocdtg\n",
      "aocdgt\n",
      "aocgtd\n",
      "aocgdt\n",
      "aotcdg\n",
      "aotcgd\n",
      "aotdcg\n",
      "aotdgc\n",
      "aotgcd\n",
      "aotgdc\n",
      "aodctg\n",
      "aodcgt\n",
      "aodtcg\n",
      "aodtgc\n",
      "aodgct\n",
      "aodgtc\n",
      "aogctd\n",
      "aogcdt\n",
      "aogtcd\n",
      "aogtdc\n",
      "aogdct\n",
      "aogdtc\n",
      "agctdo\n",
      "agctod\n",
      "agcdto\n",
      "agcdot\n",
      "agcotd\n",
      "agcodt\n",
      "agtcdo\n",
      "agtcod\n",
      "agtdco\n",
      "agtdoc\n",
      "agtocd\n",
      "agtodc\n",
      "agdcto\n",
      "agdcot\n",
      "agdtco\n",
      "agdtoc\n",
      "agdoct\n",
      "agdotc\n",
      "agoctd\n",
      "agocdt\n",
      "agotcd\n",
      "agotdc\n",
      "agodct\n",
      "agodtc\n",
      "tcadog\n",
      "tcadgo\n",
      "tcaodg\n",
      "tcaogd\n",
      "tcagdo\n",
      "tcagod\n",
      "tcdaog\n",
      "tcdago\n",
      "tcdoag\n",
      "tcdoga\n",
      "tcdgao\n",
      "tcdgoa\n",
      "tcoadg\n",
      "tcoagd\n",
      "tcodag\n",
      "tcodga\n",
      "tcogad\n",
      "tcogda\n",
      "tcgado\n",
      "tcgaod\n",
      "tcgdao\n",
      "tcgdoa\n",
      "tcgoad\n",
      "tcgoda\n",
      "tacdog\n",
      "tacdgo\n",
      "tacodg\n",
      "tacogd\n",
      "tacgdo\n",
      "tacgod\n",
      "tadcog\n",
      "tadcgo\n",
      "tadocg\n",
      "tadogc\n",
      "tadgco\n",
      "tadgoc\n",
      "taocdg\n",
      "taocgd\n",
      "taodcg\n",
      "taodgc\n",
      "taogcd\n",
      "taogdc\n",
      "tagcdo\n",
      "tagcod\n",
      "tagdco\n",
      "tagdoc\n",
      "tagocd\n",
      "tagodc\n",
      "tdcaog\n",
      "tdcago\n",
      "tdcoag\n",
      "tdcoga\n",
      "tdcgao\n",
      "tdcgoa\n",
      "tdacog\n",
      "tdacgo\n",
      "tdaocg\n",
      "tdaogc\n",
      "tdagco\n",
      "tdagoc\n",
      "tdocag\n",
      "tdocga\n",
      "tdoacg\n",
      "tdoagc\n",
      "tdogca\n",
      "tdogac\n",
      "tdgcao\n",
      "tdgcoa\n",
      "tdgaco\n",
      "tdgaoc\n",
      "tdgoca\n",
      "tdgoac\n",
      "tocadg\n",
      "tocagd\n",
      "tocdag\n",
      "tocdga\n",
      "tocgad\n",
      "tocgda\n",
      "toacdg\n",
      "toacgd\n",
      "toadcg\n",
      "toadgc\n",
      "toagcd\n",
      "toagdc\n",
      "todcag\n",
      "todcga\n",
      "todacg\n",
      "todagc\n",
      "todgca\n",
      "todgac\n",
      "togcad\n",
      "togcda\n",
      "togacd\n",
      "togadc\n",
      "togdca\n",
      "togdac\n",
      "tgcado\n",
      "tgcaod\n",
      "tgcdao\n",
      "tgcdoa\n",
      "tgcoad\n",
      "tgcoda\n",
      "tgacdo\n",
      "tgacod\n",
      "tgadco\n",
      "tgadoc\n",
      "tgaocd\n",
      "tgaodc\n",
      "tgdcao\n",
      "tgdcoa\n",
      "tgdaco\n",
      "tgdaoc\n",
      "tgdoca\n",
      "tgdoac\n",
      "tgocad\n",
      "tgocda\n",
      "tgoacd\n",
      "tgoadc\n",
      "tgodca\n",
      "tgodac\n",
      "dcatog\n",
      "dcatgo\n",
      "dcaotg\n",
      "dcaogt\n",
      "dcagto\n",
      "dcagot\n",
      "dctaog\n",
      "dctago\n",
      "dctoag\n",
      "dctoga\n",
      "dctgao\n",
      "dctgoa\n",
      "dcoatg\n",
      "dcoagt\n",
      "dcotag\n",
      "dcotga\n",
      "dcogat\n",
      "dcogta\n",
      "dcgato\n",
      "dcgaot\n",
      "dcgtao\n",
      "dcgtoa\n",
      "dcgoat\n",
      "dcgota\n",
      "dactog\n",
      "dactgo\n",
      "dacotg\n",
      "dacogt\n",
      "dacgto\n",
      "dacgot\n",
      "datcog\n",
      "datcgo\n",
      "datocg\n",
      "datogc\n",
      "datgco\n",
      "datgoc\n",
      "daoctg\n",
      "daocgt\n",
      "daotcg\n",
      "daotgc\n",
      "daogct\n",
      "daogtc\n",
      "dagcto\n",
      "dagcot\n",
      "dagtco\n",
      "dagtoc\n",
      "dagoct\n",
      "dagotc\n",
      "dtcaog\n",
      "dtcago\n",
      "dtcoag\n",
      "dtcoga\n",
      "dtcgao\n",
      "dtcgoa\n",
      "dtacog\n",
      "dtacgo\n",
      "dtaocg\n",
      "dtaogc\n",
      "dtagco\n",
      "dtagoc\n",
      "dtocag\n",
      "dtocga\n",
      "dtoacg\n",
      "dtoagc\n",
      "dtogca\n",
      "dtogac\n",
      "dtgcao\n",
      "dtgcoa\n",
      "dtgaco\n",
      "dtgaoc\n",
      "dtgoca\n",
      "dtgoac\n",
      "docatg\n",
      "docagt\n",
      "doctag\n",
      "doctga\n",
      "docgat\n",
      "docgta\n",
      "doactg\n",
      "doacgt\n",
      "doatcg\n",
      "doatgc\n",
      "doagct\n",
      "doagtc\n",
      "dotcag\n",
      "dotcga\n",
      "dotacg\n",
      "dotagc\n",
      "dotgca\n",
      "dotgac\n",
      "dogcat\n",
      "dogcta\n",
      "dogact\n",
      "dogatc\n",
      "dogtca\n",
      "dogtac\n",
      "dgcato\n",
      "dgcaot\n",
      "dgctao\n",
      "dgctoa\n",
      "dgcoat\n",
      "dgcota\n",
      "dgacto\n",
      "dgacot\n",
      "dgatco\n",
      "dgatoc\n",
      "dgaoct\n",
      "dgaotc\n",
      "dgtcao\n",
      "dgtcoa\n",
      "dgtaco\n",
      "dgtaoc\n",
      "dgtoca\n",
      "dgtoac\n",
      "dgocat\n",
      "dgocta\n",
      "dgoact\n",
      "dgoatc\n",
      "dgotca\n",
      "dgotac\n",
      "ocatdg\n",
      "ocatgd\n",
      "ocadtg\n",
      "ocadgt\n",
      "ocagtd\n",
      "ocagdt\n",
      "octadg\n",
      "octagd\n",
      "octdag\n",
      "octdga\n",
      "octgad\n",
      "octgda\n",
      "ocdatg\n",
      "ocdagt\n",
      "ocdtag\n",
      "ocdtga\n",
      "ocdgat\n",
      "ocdgta\n",
      "ocgatd\n",
      "ocgadt\n",
      "ocgtad\n",
      "ocgtda\n",
      "ocgdat\n",
      "ocgdta\n",
      "oactdg\n",
      "oactgd\n",
      "oacdtg\n",
      "oacdgt\n",
      "oacgtd\n",
      "oacgdt\n",
      "oatcdg\n",
      "oatcgd\n",
      "oatdcg\n",
      "oatdgc\n",
      "oatgcd\n",
      "oatgdc\n",
      "oadctg\n",
      "oadcgt\n",
      "oadtcg\n",
      "oadtgc\n",
      "oadgct\n",
      "oadgtc\n",
      "oagctd\n",
      "oagcdt\n",
      "oagtcd\n",
      "oagtdc\n",
      "oagdct\n",
      "oagdtc\n",
      "otcadg\n",
      "otcagd\n",
      "otcdag\n",
      "otcdga\n",
      "otcgad\n",
      "otcgda\n",
      "otacdg\n",
      "otacgd\n",
      "otadcg\n",
      "otadgc\n",
      "otagcd\n",
      "otagdc\n",
      "otdcag\n",
      "otdcga\n",
      "otdacg\n",
      "otdagc\n",
      "otdgca\n",
      "otdgac\n",
      "otgcad\n",
      "otgcda\n",
      "otgacd\n",
      "otgadc\n",
      "otgdca\n",
      "otgdac\n",
      "odcatg\n",
      "odcagt\n",
      "odctag\n",
      "odctga\n",
      "odcgat\n",
      "odcgta\n",
      "odactg\n",
      "odacgt\n",
      "odatcg\n",
      "odatgc\n",
      "odagct\n",
      "odagtc\n",
      "odtcag\n",
      "odtcga\n",
      "odtacg\n",
      "odtagc\n",
      "odtgca\n",
      "odtgac\n",
      "odgcat\n",
      "odgcta\n",
      "odgact\n",
      "odgatc\n",
      "odgtca\n",
      "odgtac\n",
      "ogcatd\n",
      "ogcadt\n",
      "ogctad\n",
      "ogctda\n",
      "ogcdat\n",
      "ogcdta\n",
      "ogactd\n",
      "ogacdt\n",
      "ogatcd\n",
      "ogatdc\n",
      "ogadct\n",
      "ogadtc\n",
      "ogtcad\n",
      "ogtcda\n",
      "ogtacd\n",
      "ogtadc\n",
      "ogtdca\n",
      "ogtdac\n",
      "ogdcat\n",
      "ogdcta\n",
      "ogdact\n",
      "ogdatc\n",
      "ogdtca\n",
      "ogdtac\n",
      "gcatdo\n",
      "gcatod\n",
      "gcadto\n",
      "gcadot\n",
      "gcaotd\n",
      "gcaodt\n",
      "gctado\n",
      "gctaod\n",
      "gctdao\n",
      "gctdoa\n",
      "gctoad\n",
      "gctoda\n",
      "gcdato\n",
      "gcdaot\n",
      "gcdtao\n",
      "gcdtoa\n",
      "gcdoat\n",
      "gcdota\n",
      "gcoatd\n",
      "gcoadt\n",
      "gcotad\n",
      "gcotda\n",
      "gcodat\n",
      "gcodta\n",
      "gactdo\n",
      "gactod\n",
      "gacdto\n",
      "gacdot\n",
      "gacotd\n",
      "gacodt\n",
      "gatcdo\n",
      "gatcod\n",
      "gatdco\n",
      "gatdoc\n",
      "gatocd\n",
      "gatodc\n",
      "gadcto\n",
      "gadcot\n",
      "gadtco\n",
      "gadtoc\n",
      "gadoct\n",
      "gadotc\n",
      "gaoctd\n",
      "gaocdt\n",
      "gaotcd\n",
      "gaotdc\n",
      "gaodct\n",
      "gaodtc\n",
      "gtcado\n",
      "gtcaod\n",
      "gtcdao\n",
      "gtcdoa\n",
      "gtcoad\n",
      "gtcoda\n",
      "gtacdo\n",
      "gtacod\n",
      "gtadco\n",
      "gtadoc\n",
      "gtaocd\n",
      "gtaodc\n",
      "gtdcao\n",
      "gtdcoa\n",
      "gtdaco\n",
      "gtdaoc\n",
      "gtdoca\n",
      "gtdoac\n",
      "gtocad\n",
      "gtocda\n",
      "gtoacd\n",
      "gtoadc\n",
      "gtodca\n",
      "gtodac\n",
      "gdcato\n",
      "gdcaot\n",
      "gdctao\n",
      "gdctoa\n",
      "gdcoat\n",
      "gdcota\n",
      "gdacto\n",
      "gdacot\n",
      "gdatco\n",
      "gdatoc\n",
      "gdaoct\n",
      "gdaotc\n",
      "gdtcao\n",
      "gdtcoa\n",
      "gdtaco\n",
      "gdtaoc\n",
      "gdtoca\n",
      "gdtoac\n",
      "gdocat\n",
      "gdocta\n",
      "gdoact\n",
      "gdoatc\n",
      "gdotca\n",
      "gdotac\n",
      "gocatd\n",
      "gocadt\n",
      "goctad\n",
      "goctda\n",
      "gocdat\n",
      "gocdta\n",
      "goactd\n",
      "goacdt\n",
      "goatcd\n",
      "goatdc\n",
      "goadct\n",
      "goadtc\n",
      "gotcad\n",
      "gotcda\n",
      "gotacd\n",
      "gotadc\n",
      "gotdca\n",
      "gotdac\n",
      "godcat\n",
      "godcta\n",
      "godact\n",
      "godatc\n",
      "godtca\n",
      "godtac\n"
     ]
    }
   ],
   "source": [
    "from itertools import permutations\n",
    "string = []\n",
    "for i in permutations(list, len(list)):\n",
    "    string.append(i)\n",
    "print('共' + str(len(string)) + '种排列')\n",
    "for strings in string:\n",
    "    print(''.join(strings))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "P-1.30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def divide(a):\n",
    "    while a >= 2:\n",
    "        a = a//2\n",
    "        print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50\n",
      "25\n",
      "12\n",
      "6\n",
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "divide(100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "P-1.31\n",
    "\n",
    "找零程序。找零，用取整除法，注意对货币进行排序后再遍历。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def change(a, b, list):\n",
    "    list_change = []\n",
    "    c = a - b\n",
    "    list = sorted(list, reverse = True)\n",
    "    for i in list:\n",
    "        list_change.append(c//i)\n",
    "        c -= (c//i)*i\n",
    "    return list_change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 0, 1, 0, 4]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list = [1, 5, 10, 20, 50, 100]\n",
    "change(200, 136, list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "P-1.32\n",
    "\n",
    "计算器，跳过。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "P-1.33\n",
    "\n",
    "计算器，跳过。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "P-1.35"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "def probability(n):\n",
    "    list = []\n",
    "    for i in range(0, 10000):\n",
    "        a = np.random.randint(0, 365, n)\n",
    "        list.append(len(a) != len(set(a)))\n",
    "    return np.mean(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5056\n",
      "0.9689\n"
     ]
    }
   ],
   "source": [
    "print(probability(23))\n",
    "print(probability(50))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "P-1.36\n",
    "\n",
    "统计列表中不同元素的数量，R语言中可以直接用`table()`搞定，python却没有这么简单的途径。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_word(string):\n",
    "    a = string.split()\n",
    "    word = {}\n",
    "    for i in set(a):\n",
    "        word[i] = a.count(i)\n",
    "    return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'basketball': 1, 'I': 1, 'like': 1, 'playing': 1}\n",
      "{'you': 1, 'How': 1, 'do': 2}\n"
     ]
    }
   ],
   "source": [
    "print(count_word('I like playing basketball'))\n",
    "print(count_word('How do you do'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
